//
//  DSAlertController.swift
//  DSP
//
//  Created by tucici on 2024/5/22.
//

import UIKit
import TCCKit
class DSAlertController: DSViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    ///必须要加上这个方法，否则self.modalPresentationStyle = .overFullScreen不生效
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        //        self.view.layoutIfNeeded()
        self.modalPresentationStyle = .overFullScreen
        self.view.backgroundColor = "#000000".color(0.6)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func initUI(){}
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let view = self.firstResponder(self.view)
        if view != nil{
            super.touchesEnded(touches, with: event)
            self.view.endEditing(true)
        } else{
            let touch = (touches as NSSet).allObjects.last as! UITouch
            if touch.view!.isDescendant(of: self.view.subviews.first ?? UIView()) == false && self.currentVC() == self
            {
                self.dismiss(animated:false, completion: nil)
            }
        }
    }
    
    func hidden(_ show:Bool=true){
        if show{
            CLHUD(type: .loading ,callBack: {[weak self] in
                self?.dismiss(animated: false)
            })
        }else{
            DispatchQueue.main.asyncAfter(deadline: .now()+1.0, execute: {[weak self] in
                self?.dismiss(animated: false)
            })
        }
    }
    
}

extension UIViewController{
    func firstResponder(_ view:UIView)->UIView?{
        if view.isFirstResponder{ return view}
        for subView in view.subviews{
            let responder = self.firstResponder(subView)
            if responder != nil{ return responder}
        }
        return nil
    }
}
